package com.onesports.intelligent.k12.polarlight.service.operation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.onesports.framework.kit.common.util.ParamUtils;
import com.onesports.intelligent.k12.polarlight.domain.dto.operation.PageOperationHomeDTO;
import com.onesports.intelligent.k12.polarlight.domain.entity.market.Message;
import com.onesports.intelligent.k12.polarlight.domain.entity.market.MessageNotify;
import com.onesports.intelligent.k12.polarlight.domain.entity.organization.OrganizationStaff;
import com.onesports.intelligent.k12.polarlight.domain.vo.operation.PageOperationMessageVO;
import com.onesports.intelligent.k12.polarlight.mapper.market.MessageMapper;
import com.onesports.intelligent.k12.polarlight.mapper.market.MessageNotifyMapper;
import com.onesports.intelligent.k12.polarlight.mapper.organization.OrganizationStaffMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wangcx
 **/
@Service
@RequiredArgsConstructor
public class MessageService extends ServiceImpl<MessageMapper, Message> {

    private final MessageMapper mapper;
    private final MessageNotifyMapper messageNotifyMapper;
    private final OrganizationStaffMapper organizationStaffMapper;

    /**
     * 根据条件模糊搜索运营端消息通知
     *
     * @param page 分页参数
     * @param dto  入参 消息类型和消息内容
     * @return 消息PageOperationMessageVO
     */
    public IPage<PageOperationMessageVO> page(Page<PageOperationHomeDTO> page, PageOperationHomeDTO dto) {
        if (ParamUtils.isNotEmpty(dto.getOrganizationId())) {
            dto.setOrganizationStaffId(getStaffIdByOrgId(dto.getOrganizationId()));
        }
        Long count;
        IPage<PageOperationMessageVO> result = this.mapper.page(page, dto);
        if (ParamUtils.isNotEmpty(result.getRecords())) {
            count = this.mapper.countNoRead(dto);
            for (PageOperationMessageVO record : result.getRecords()) {
                record.setNoReadNumber(count);
            }
        }
        return result;

    }

    /**
     * function：根据运营团队Id获取当前负责人Id
     *
     * @param orgId    运营团队Id
     * @return staffId 运营团队负责人Id
     * @author wangcx
     * @since 2022/7/27 9:28
     */
    public String getStaffIdByOrgId(String orgId) {
        return this.organizationStaffMapper.selectOne(new LambdaQueryWrapper<OrganizationStaff>()
                .eq(OrganizationStaff::getOrganizationId, orgId)
                .eq(OrganizationStaff::getIsPrincipal, true)).getId();
    }


    /**
     * function：删除消息以及消息通知
     *
     * @param id 消息id
     * @return
     * @author wangcx
     * @since 2022/7/15 11:26
     */
    public void del(String id) {
        this.mapper.deleteById(id);
        // 删除消息通知表
        LambdaQueryWrapper<MessageNotify> lam = new LambdaQueryWrapper<>();
        lam.eq(MessageNotify::getMessageId, id);
        this.messageNotifyMapper.delete(lam);
    }

    /**
     * 标记指定消息为已读
     *
     * @param id 消息id
     */
    public void read(String id) {
        LambdaUpdateWrapper<MessageNotify> lam = new LambdaUpdateWrapper<>();
        lam.eq(MessageNotify::getMessageId, id).set(MessageNotify::getReaded, true);
        this.messageNotifyMapper.update(null, lam);
    }

    /**
     * 标记所有消息为已读
     *
     * @param meTypes 消息端
     */
    public void readAll(List<String> meTypes, String orgId) {
        String staffId = null;
        if (ParamUtils.isNotEmpty(orgId)) {
            staffId = this.getStaffIdByOrgId(orgId);
        }

        // 获取运营端的消息列表
        List<String> messageIds = this.mapper.selectList(new LambdaQueryWrapper<Message>()
                .eq(Message::getDeleted, false)
                .in(Message::getMsgType, meTypes))
                .stream().map(Message::getId).collect(Collectors.toList());
        LambdaUpdateWrapper<MessageNotify> lam = new LambdaUpdateWrapper<>();
        lam.set(MessageNotify::getReaded, true).eq(MessageNotify::getDeleted, false)
                .in(MessageNotify::getMessageId, messageIds)
                .eq(ParamUtils.isNotEmpty(staffId), MessageNotify::getOrganizationStaffId, staffId);
        this.messageNotifyMapper.update(null, lam);
    }

    /**
     * 添加消息到消息表并且添加到消息通知表
     */
    public void addMessage(Message message, MessageNotify messageNotify) {
        this.mapper.insert(message);
        this.messageNotifyMapper.insert(messageNotify);
    }

}

